package com.awspaass.user.apps.tempcar;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

@Controller
public class AppCmdDriver {

	@Mapping("com.awspaas.user.apps.shhtaerospaceindustrial_driverGetMission")
	public String driverGetMission(UserContext uc, int roleType, String bDate, String eDate, int page, int pageCount,
			int taskType) {

		JSONObject returnData = new JSONObject();
	

			try {
				String userId = uc.getUID();
				String sid = uc.getSessionId();
				if (page < 1) {
					returnData.put("status", "1");
					returnData.put("message", "请传入大于等于1的起始页！");
					return returnData.toString();
				}
				// 获取起始条数和结束条数
				int start = (page - 1) * pageCount + 1;
				int end = page * pageCount;
				String xcqr = "";// 行车确认是否查全部
				if (!bDate.equals("") && !eDate.equals("")) {
					xcqr = "AND B.UDATE >= TO_DATE('" + bDate + "','yyyy-MM-dd') AND B.UDATE <= TO_DATE('" + eDate
							+ "','yyyy-MM-dd')";
				}
				String queryXcqrTaskInfo = "";

				queryXcqrTaskInfo = "SELECT * FROM (SELECT TT.*, ROWNUM AS rowno FROM (SELECT (CASE WHEN B.SJLXFS IS NOT NULL THEN CONCAT("
						+ "CONCAT(CONCAT(B.SJXM,'('),B.SJLXFS),')') ELSE CONCAT(B.SJXM,B.SJLXFS) END) SJXX,B.CPH,B.APPLYUSERNAME,B.APPLYUSERCELLPHONE,B.UDATE,B.CFSJ,B.FHSJ,B.TOTALMONEY,B.VEHICLETYPE,"
						+ "(CASE WHEN B.MISSIONSTATUS=0 THEN '未派单' WHEN B.MISSIONSTATUS=1 THEN '已派单' WHEN B.MISSIONSTATUS=2 THEN '已接单' WHEN B.MISSIONSTATUS"
						+ "=3 THEN '待结算' WHEN B.MISSIONSTATUS=4 THEN '待确认' WHEN B.MISSIONSTATUS=5 THEN '已确认' WHEN B.MISSIONSTATUS=6 THEN '已取消' ELSE '' END) MISSIONSTATUS,A.PROCESSINSTID,A.ID,'1' RWZT,B.ORDERDATE FROM BO_EU_SH_VEHICLEORDER_MISSION B LEFT JOIN WFC_TASK A ON "
						+ "A.PROCESSINSTID = B.BINDID WHERE B.SJZH = '" + userId + "' " + xcqr
						+ " AND B.MISSIONSTATUS IN (1,2,3) AND A.DISPATCHID IS NOT NULL AND A.TASKTITLE NOT LIKE '%空标题%'"
						+ " ORDER BY B.UDATE) TT WHERE ROWNUM <= " + end + ") table_alias WHERE table_alias.rowno >= "
						+ start + "";

				System.out.println("####TaskType==0########queryXcqrTaskInfo");
				System.out.println(queryXcqrTaskInfo);

				String portalUrl = SDK.getPortalAPI().getPortalUrl();// http://localhost:8088/portal
				JSONArray jsonXcqr = new JSONArray();

				if (!queryXcqrTaskInfo.equals("")) {
					List<Map<String, Object>> xcqrTaskInfoList = DBSql.query(queryXcqrTaskInfo,
							new ColumnMapRowMapper(), new Object[] {});
					if (xcqrTaskInfoList == null || xcqrTaskInfoList.isEmpty()) {
						returnData.put("status", "0");
						returnData.put("jsonXcqr", jsonXcqr);
					} else {
						for (int i = 0; i < xcqrTaskInfoList.size(); i++) {
							JSONObject jsonXcqrObj = new JSONObject();
							Map<String, Object> xcqrMap = xcqrTaskInfoList.get(i);
							String udate = CoreUtil.objToStr(xcqrMap.get("UDATE"));
							String sjxx = CoreUtil.objToStr(xcqrMap.get("SJXX"));// 司机姓名（联系方式）
							String cfsj = CoreUtil.objToStr(xcqrMap.get("CFSJ"));// 出车时间
							String fhsj = CoreUtil.objToStr(xcqrMap.get("FHSJ"));// 返回时间
							String sjjd = CoreUtil.objToStr(xcqrMap.get("ORDERDATE"));// 司机接单
							String APPLYUSERCELLPHONE = CoreUtil.objToStr(xcqrMap.get("APPLYUSERCELLPHONE"));
							
							String APPLYUSERNAME = CoreUtil.objToStr(xcqrMap.get("APPLYUSERNAME"));
							String carNo = CoreUtil.objToStr(xcqrMap.get("CPH"));
							String missionStatus = CoreUtil.objToStr(xcqrMap.get("MISSIONSTATUS"));
							if (!cfsj.equals("")) {
								cfsj = cfsj.substring(0, 16);
							}
							if (!fhsj.equals("")) {
								fhsj = fhsj.substring(0, 16);
							}
							if (!sjjd.equals("")) {
								sjjd = sjjd.substring(0, 16);
							}
							String fyzj = CoreUtil.objToStr(xcqrMap.get("TOTALMONEY"));// 费用总计
							String cx = CoreUtil.objToStr(xcqrMap.get("VEHICLETYPE"));// 车型
							String zt = CoreUtil.objToStr(xcqrMap.get("MISSIONSTATUS"));// 状态
							String processInstId = CoreUtil.objToStr(xcqrMap.get("PROCESSINSTID"));// 流程实例ID
							String taskInstId = CoreUtil.objToStr(xcqrMap.get("ID"));// 任务实例Id
							String rwzt = CoreUtil.objToStr(xcqrMap.get("RWZT"));// 1：待办|2：已办
							// https://www.ht804dzs.cn/portal/r/w?sid=c82f49da-e292-44d5-9a63-87d1c72db466&
							// cmd=CLIENT_BPM_FORM_MAIN_PAGE_OPEN&processInstId=b3cddb87-ebf4-4546-82ee-6d24543079c0&openState=1
							// &taskInstId=ff8ea21c-ec24-42fa-8293-6a8a53f459de&displayToolbar=true
							String url = portalUrl + "/r/w?sid=" + sid
									+ "&cmd=CLIENT_BPM_FORM_MAIN_PAGE_OPEN&processInstId=" + processInstId
									+ "&openState=1&taskInstId=" + taskInstId + "&displayToolbar=true";
							jsonXcqrObj.put("applyUserName",APPLYUSERNAME);
							jsonXcqrObj.put("applyPhone",APPLYUSERCELLPHONE);
							jsonXcqrObj.put("carNo",carNo);
							jsonXcqrObj.put("useDate",udate);
							jsonXcqrObj.put("missionStatus",missionStatus);
							jsonXcqrObj.put("carType",cx);
							
							jsonXcqrObj.put("sjxx", sjxx);
							jsonXcqrObj.put("cfsj", cfsj);
							jsonXcqrObj.put("fhsj", fhsj);
							jsonXcqrObj.put("sjjd", sjjd);
							jsonXcqrObj.put("fyzj", fyzj);
							cx = SDK.getDictAPI().getValue("com.awspaas.user.apps.shhtaerospaceindustrial", "shcartype",
									cx, "CNNAME");
							jsonXcqrObj.put("cx", cx);
							jsonXcqrObj.put("zt", zt);
							jsonXcqrObj.put("rwzt", rwzt);
							jsonXcqrObj.put("processInstId", processInstId);
							jsonXcqrObj.put("url", url);
							jsonXcqrObj.put("udate", udate);
							jsonXcqr.add(jsonXcqrObj);
						}
					}
				}

				returnData.put("status", "0");
				returnData.put("jsonXcqr", jsonXcqr);
				returnData.put("jsonClyd", "");
			} catch (Exception e) {
				e.printStackTrace();
				returnData.put("status", "1");
				returnData.put("message", e.getMessage());
			}

		
		return returnData.toString();
	
	}
}
